#!/usr/bin/python

# Stutedoku version 0.0.1 <http://www.rprego.com>
# Copyright (C) 2011 Richard Prego <rprego@rprego.com>
# Last modified 2011-04-06
#
# This file is part of Stutedoku.
#
# Stutedoku is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Stutedoku is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Stutedoku.  If not, see <http://www.gnu.org/licenses/>.

import random

class Sudoku(object):
       
    def __init__(self):
        """Initialize empty Sudoku grid."""
        self.clear_grid()        
        
    def clear_grid(self):
        self.grid = []
        for row in range(0, 9):
            self.grid.append(['-', '-', '-', '-', '-', '-', '-', '-', '-'])
    
    def print_grid(self):
        for row in self.grid:
            for item in row:
                print item,
            print ""
                
    def is_in_row(self, row, number):
        if number in self.grid[row]:
            return 1
        else:
            return 0
        
    def is_in_col(self, col, number):
        for row in range(0, 9):
            if self.grid[row][col] == number:
                return 1
        return 0
    
    def is_in_box(self, rbox, cbox, number):
        for row in range(3 * rbox, 3 * rbox + 3):
            for col in range(3 * cbox, 3 * cbox + 3):
                if self.grid[row][col] == number:
                    return 1
        return 0
    
    def fill_grid(self):
        random.seed()
        col = 0        
        while col < 9:
            possible_nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
            row = 0
            try_number = 0
            while row < 9 and try_number < 200:
                bad_nums = 0
                stop = 0
                for element in range(0, len(possible_nums)):
                    if (self.is_in_col(col, possible_nums[element]) == 1 or self.is_in_row(row, possible_nums[element]) == 1 or self.is_in_box(int(round(row / 3)), int(round(col / 3)), possible_nums[element]) == 1):
                        bad_nums += 1
                if bad_nums == len(possible_nums):
                    for row_2 in range(0, 9):
                        self.grid[row_2][col] = '-'
                        possible_nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
                        stop = 1
                        row = 0
                    try_number += 1
                if stop == 0:
                    number = random.randint(1, 9)
                    while (self.is_in_col(col, number) == 1 or self.is_in_row(row, number) == 1 or self.is_in_box(int(round(row / 3)), int(round(col / 3)), number) == 1):
                        random.seed()
                        number = random.randint(1, 9)
                    self.grid[row][col] = number
                    possible_nums.remove(number)
                    row += 1
            col += 1
            if try_number == 200:
                self.clear_grid()
                col = 0
